/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p>Project: app2022 - T27
 * <p>Powered by webrx On 2022-02-08 09:06:45
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class T27 {
    AtomicInteger num = new AtomicInteger(50);

    public static void main(String[] args) {
        var t = new T27();
        for (int i = 0; i < 6; i++) {
            new Thread(t::seller, "T" + i).start();
        }
    }

    void seller() {
        String tn = Thread.currentThread().getName();
        while (true) {
            synchronized (this) {
                if (num.get() > 0) {
                    num.decrementAndGet();
                    System.out.printf("%s 售出一张票，剩余%d张票。 %n", tn, num.get());
                } else {
                    break;
                }
                try {
                    Thread.sleep(150);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
